package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.domain.Category;
import com.itheima.domain.Dish;
import com.itheima.domain.DishFlavor;
import com.itheima.dto.DishDto;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品
     * @param DishDto
     * @return
     */
    @PostMapping
    public R save(@RequestBody DishDto DishDto) {

        dishService.saveWithFlavor(DishDto);

        return R.success("新菜品增加成功!");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R page(int page, int pageSize, String name) {
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        Page<DishDto> DtoPage = new Page<>();
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null, Dish::getName, name);
        dishService.page(pageInfo, lqw);

        BeanUtils.copyProperties(pageInfo, DtoPage, "records");
        List<Dish> records = pageInfo.getRecords();
        ArrayList<DishDto> dishDtolist = new ArrayList<>();
        for (Dish record : records) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(record, dishDto);
            Category Id = categoryService.getById(record.getCategoryId());
            if (Id != null) {
                dishDto.setCategoryName(Id.getName());
            }
            dishDtolist.add(dishDto);
        }
        DtoPage.setRecords(dishDtolist);

        return R.success(DtoPage);
    }

    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id) {

        DishDto dishDto = dishService.getByIdWithFlavor(id);

        return R.success(dishDto);
    }

    /**
     * 修改菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> Update(@RequestBody DishDto dishDto) {

        dishService.updateWithFlavor(dishDto);

        return R.success("修改成功!");
    }

    /**
     * 菜品删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long[] ids) {
        log.info(ids.toString());

        dishService.DeleteByIds(ids);

        return R.success("删除成功!");
    }

    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable Integer status, Long[] ids) {

        dishService.UpdateStatusById(status, ids);

        return R.success("修改成功!");
    }
    /**
     * 根据条件查询对应的菜品数据
     * @param dish
     * @return
     */
    /*@GetMapping("/list")
    public R<List<Dish>> list(Dish dish) {

        LambdaQueryWrapper<Dish> dishlqw = new LambdaQueryWrapper<>();
        dishlqw.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
        dishlqw.eq(Dish::getStatus, 1);
        dishlqw.orderByDesc(Dish::getUpdateTime);

        List<Dish> dishList = dishService.list(dishlqw);

        return R.success(dishList);
    }*/

    /**
     * 根据条件查询对应的菜品数据
     *
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish) {

        LambdaQueryWrapper<Dish> dishlqw = new LambdaQueryWrapper<>();
        dishlqw.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
        dishlqw.eq(Dish::getStatus, 1);
        dishlqw.orderByDesc(Dish::getUpdateTime);

        List<Dish> dishList = dishService.list(dishlqw);

        ArrayList<DishDto> resultList = new ArrayList<>();
        for (Dish dish1 : dishList) {
            Long dishId = dish1.getId();
            DishDto dishDto = new DishDto();
            LambdaQueryWrapper<DishFlavor> dishFlavorlqw = new LambdaQueryWrapper<>();
            dishFlavorlqw.eq(DishFlavor::getDishId, dishId);
            List<DishFlavor> dishFlavors = dishFlavorService.list(dishFlavorlqw);
            dishDto.setFlavors(dishFlavors);
            BeanUtils.copyProperties(dish1, dishDto);
            resultList.add(dishDto);
        }

        return R.success(resultList);
    }
}
